define(function(require, exports, module) {

    var $ = require('$'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        ContactModel = require('model/contact'),
        ContactCollection = require('collection/contact');

    module.exports = Backbone.View.extend({
        el: '.page[data-page=contactView]',
        events: {
            //未建立好友关系时为“加为好友”按钮
            'click .btn-add-friend': 'onSetRemarkAndSave',
            //已建立好友关系时为“修改备注”按钮
            'click .btn-set-remark': 'onSetRemarkAndSave',
            //“解除关系”按钮
            'click .btn-remove-friend': 'onRemoveFriend',
            //约他
            'click .btn-contact': 'onContact'
        },
        template: require('tpl/contact/view.handlebars'),
        initialize: function(options) {
            var self = this;
            
            //传入gomac
            this.gomac = options.gomac;

            //新建联系人模型
            this.model = new ContactModel;
            //设置collection
            this.model.collection = new ContactCollection;
            //设置联系人id
            this.model.id = options.objectId;
            //本地获取联系人
            this.model.fetch({
                success: function(model, response, options) {
                    //获取成功
                    self.$el.find('.page-content').html(self.template({
                        model: model.toJSON(),
                        added: true
                    }));
                },
                error: function(model, response, options) {
                    //本地获取失败，进行获取远程数据
                    console.log('获取本地联系人时出错',model,response,options);

                    //TODO: 远程搜索
                    var Users = require('data/users');
                    var user = _.findWhere(Users, {objectId: model.id});

                    if(user) {
                        self.model.loadFromRemote(user);
                        if(self.model.isValid()) {
                            self.$el.find('.page-content').html(self.template({
                                model: self.model.toJSON(),
                                added: false
                            }));
                        } else {
                            self.gomac.alert(self.model.validationError);
                        }
                    }
                    else {
                        self.gomac.alert('获取联系人时出错');
                    }
                }
            });
        },
        saveModel: function() {
            var self = this;
            self.model.save(null, {
                success: function(model, resp, options) {
                    console.log('保存好友成功', model, resp, options);
                    self.gomac.mainView.router.reloadPage('pages/contacts/view.html?id='+model.id);
                },
                error: function(model, resp, options) {
                    console.log('保存好友出错', model, resp, options);
                }
            });
        },
        onSetRemarkAndSave: function(e) {
            var self = this;
            var callbackOk = function(val) {
                self.model.setRemark(val);
                self.saveModel();
            };
            var callbackCancel = function(val) {
                self.saveModel();
            };
            self.gomac.prompt('请输入备注名', callbackOk, callbackCancel);
        },
        onRemoveFriend: function(e) {
            var self = this;
            var callbackOk = function(val) {
                self.model.destroy({
                    success: function(model, resp, options) {
                        console.log('删除好友成功', model, resp, options);
                        self.gomac.mainView.router.loadPage('pages/contacts.html');
                    },
                    error: function(model, resp, options) {
                        console.log('删除好友出错', model, resp, options);
                    }
                });
            };
            var callbackCancel = function(val) {};
            self.gomac.confirm('确定删除好友 '+self.model.get('remark')+' 吗？', callbackOk, callbackCancel)
        },
        onContact: function(e) {
            //TODO: 
            var self = this;
            self.gomac.alert('约吗？');
        }
    });
});